#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "kernel.h"

// #define block_size 2000
#define block_size pow(2,24)

void usage()
{
    fprintf(stdout, 
    "\npfb [option] dada_file\n\n"
    "dada_file          of the dada file name for caspsr backend.\n"
    "\n"
    "-m unsigned int    the num of tab for pfb (default=64).\n"
    "-n unsigned int    the num of FFT for pfb (default=16).\n"
    "-c coeffs_file     of the coefficient file name for pfb. if not, the program automatically generates.\n"
    "-s                 if be set, oversampling-pfb be set on.\n"
    "-h                 print help text.\n\n");
}


int main(int argc,char *argv[])
{
    /* data file */
    char * dada_name;

    /* pfb coeffs file */
    char * coeffs_name = NULL;

    char * output_name = "profile.txt";

    int ntap = 64;
    int nfft = 16;
    int opfb_factor = 1;
    int option = 0;

    while((option = getopt(argc, argv, "ht:n:c:s")) != -1){
        switch (option) {
        case 'h':
            usage();
            exit(0);
        case 't':
            ntap = atoi(optarg);
            break;
        case 'n':
            nfft = atoi(optarg);
            break;
        case 'c':
            coeffs_name = optarg;
            break;
        case 's':
            opfb_factor = 2;
            break;
        default:
            usage();
            exit(0);
        }
    }

    /* no arguments */
    if (argc <= optind){
        fprintf(stderr, "ERROR: Data file not specified!\n");
        usage();
        exit(-1);
    }else{
        /* get data_name */
        dada_name = argv[optind];
    }

    printf("dada_name = %s\n",dada_name);
    printf("coeffs_name = %s\n",coeffs_name);
    printf("nfft = %d, ntap = %d, opfb_factor = %d\n",nfft,ntap,opfb_factor);

    clock_t start,end;

    start = clock();
    operation(dada_name, output_name, coeffs_name, ntap, nfft, block_size, opfb_factor);
    end = clock();
    
    printf("all time = %f seconds\n",(double)(end - start) / CLOCKS_PER_SEC);

    exit(0);
}